package com.luhuiguo.cms.server.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.luhuiguo.cms.server.service.CmsService;
import com.luhuiguo.cms.server.util.PathUtils;
import com.luhuiguo.cms.shared.model.ChannelModel;

public class ChannelHandler implements Controller {

    @Autowired
    private CmsService cmsService;

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uri = request.getRequestURI();
        String viewName = PathUtils.getChannelViewName(uri);
        String path = PathUtils.getChannelPath(uri);

        ModelAndView mav = new ModelAndView(viewName);
        ChannelModel channel = cmsService.getChannelByPath(path);
        mav.addObject("channel", channel);
        return mav;
    }

}
